import request from '../utils/request';

/**
 * 新增一条报警记录
 * @param {string} deviceUuid 设备uuid
 * @param {string} threshold 阈值
 * @param {string} value 实测值
 */
export function createAlarmHistoryApi(deviceUuid, threshold, value) {
  return request({
    url: '/alarm_histories',
    method: 'post',
    data: {
      device_uuid: deviceUuid,
      threshold,
      value
    }
  });
}

/**
 * 批量删除报警记录
 * @param {*} uuidList 
 */
export function deleteAlarmLogListApi(uuidList) {
  return request({
    url: '/alarm_histories',
    method: 'delete',
    data: {
      uuid_list: uuidList
    }
  });
}

/**
 * 更新报警记录
 * @param {string} uuid 报警记录uuid
 * @param {string} alarmStatus 报警状态
 */
export function updateAlarmHistoryApi(uuid, alarmStatus) {
  return request({
    url: '/alarm_histories/' + uuid,
    method: 'put',
    data: {
      alarm_status: alarmStatus
    }
  });
}

/**
 * 获取报警记录
 * @param {number} pageIndex 分页页码
 * @param {number} pageSize 分页每页数量
 * @param {string} sortType 排列顺序
 * @param {string} orderBy 排序依据
 * @param {string} name 报警规则名称
 * @param {string} deviceUuid 设备uuid
 * @param {string} alarmType 报警类型
 * @param {string} alarmStatus 报警处理状态
 */
export function getAlarmHistoryApi(pageIndex, pageSize, sortType, orderBy, deviceUuid, alarmType, alarmStatus) {
  return request({
    url: '/alarm_histories',
    method: 'get',
    params: {
      page_index: pageIndex,
      page_size: pageSize,
      sort_type: sortType,
      order_by: orderBy,
      device_uuid: deviceUuid || null,
      alarm_type: alarmType || null,
      alarm_status: alarmStatus || null
    }
  });
}

export function getAlarmHistoryLatestListApi() {
  const baseUrl = (process.env.VUE_APP_BASE_API).split('/admin')[0];
  return request({
    url: baseUrl + '/dashboard/alarm_histories/latest',
    method: 'get'
  });
}
